using System;
using System.Collections.Generic;

using Clandestine.Base;
using Clandestine.Graphics;
using Clandestine.Engines.Field.Collision;

namespace Shlick
{
	public class CollisionTool : Tool	
	{
		public MapDocument MapDocument { get; private set; }
		public Graphics Graphics { get { return MapDocument.Graphics; } }
		
		Hull maurine = null;
		List<Sprite> visibad = new List<Sprite>();
		Sprite spoon;
		
		public CollisionTool(MapDocument md)
		{
			this.MapDocument = md;
		}
		
		#region ITool implementation
		public event ToolCompleteHandler ToolComplete;

		public override void MouseDownLeft (int mouseX, int mouseY, int tileX, int tileY)
		{
			
		}

		public override void MouseUpLeft (int mouseX, int mouseY, int tileX, int tileY)
		{
			
			Sprite spo = new Sprite(Graphics, "vertex.png");
			//spo.Visible = true;
			//spo.Transform.ScaleX = 1;
			//spo.Transform.ScaleY = 1;
			//spo.Transform.ScaleUniform = 20;
			//spo.Depth = 999; // ONE MEEEEEELION
			//spo.Color = Color4.White;
			Log.i(mouseX.ToString() + "," + mouseY.ToString());
			spo.Transform.X = mouseX;
			spo.Transform.Y = mouseY;
			spo.Transform.Origin = new System.Drawing.Point(6, 6);
			visibad.Add(spo);
			MapDocument.ToolManager.ActiveLevel.SkyRenderList.Add(spo);
		}

		public override void MouseDownRight (int mouseX, int mouseY, int tileX, int tileY)
		{
			//throw new NotImplementedException ();
		}

		public override void MouseUpRight (int mouseX, int mouseY, int tileX, int tileY)
		{
			Log.w("I! CREATE! HULL!!!");
			
			List<System.Drawing.Point> poos = new List<System.Drawing.Point>();
			
			foreach (Sprite sp in visibad)
			{
				poos.Add(new System.Drawing.Point(sp.Transform.X, sp.Transform.Y));
				//sp.Dispose();
			}
			
			//visibad.Clear();
			
			maurine = new Hull(poos);
			spoon = maurine.Render(MapDocument.ToolManager.ActiveLevel.SkyRenderList);
			HullDebugger hd = new HullDebugger(MapDocument.ToolManager.ActiveLevel.SkyRenderList, maurine);
			//MapDocument.ToolManager.ActiveLevel.SkyRenderList.Add(maurine);
		}

		public override void MouseMove (int mouseX, int mouseY, int tileX, int tileY)
		{
			if (maurine != null)
			{
				if (maurine.Contains(new System.Drawing.Point(mouseX, mouseY)))
					spoon.Color = Color4.Green;
				else
					spoon.Color = Color4.Red;
			}
		}

		public override void Activated (params object[] args)
		{
			//throw new NotImplementedException ();
		}

		public override void Deactivated ()
		{
			//throw new NotImplementedException ();
		}

		public override string Name {
			get {
				return "COLLISION-O-TRON (NOW WITH 191.8 FPS!)";
			}
		}
		#endregion
	}
}
